﻿package com.hoopchina.weibo.ui;
import java.io.File;
import java.util.ArrayList;

import com.hoopchina.news.R;
import com.hoopchina.data.ListBaseAdapter;
import com.hoopchina.meta.BindItem;
import com.hoopchina.util.UrlUtils;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.graphics.BitmapFactory;

/**
 * 
 * @author neusoft
 *
 */
class BindingListAdaptor extends ListBaseAdapter {
	private Context mContext;
	private static final String HEAD_IMAGE_SAVE_PATH = "/BindImages/";
	public BindingListAdaptor(Context aContext, ArrayList<?> aData) {
		super(aContext, aData);
		mContext = aContext;
		mResource = R.layout.mybindingitem;
	}

	class ListItemHolder {
		ImageView mImage;
		int mType;
		TextView mScreenName;
		TextView mSite;
		Button mCancelBtn;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ListItemHolder holder = null;
		BindItem item = (BindItem) mData.get(position);
		if (convertView == null) {
			convertView = mLayoutInflater.inflate(mResource, null);
			holder = new ListItemHolder();
			holder.mImage = (ImageView) convertView
					.findViewById(R.id.userHeadImage);
			holder.mScreenName = (TextView) convertView
					.findViewById(R.id.userName);
			holder.mSite = (TextView) convertView
					.findViewById(R.id.weiboSource);
			holder.mCancelBtn = (Button) convertView
					.findViewById(R.id.cancelBindingBtn);
			convertView.setTag(holder);
		} else {
			holder = (ListItemHolder) convertView.getTag();
		}
		holder.mType = item.getType();
		holder.mScreenName.setText(item.getScreenName());
		holder.mSite.setText(item.getSite());
		String imagePath = mContext.getCacheDir()+ HEAD_IMAGE_SAVE_PATH + UrlUtils.getNameFromUrl( item.getHeadUrl() );
		File file = new File( imagePath );
		if (file.exists()) {
			holder.mImage.setImageBitmap(BitmapFactory.decodeFile(file.getPath()));
		}
		final int type = holder.mType;
		final int pos = position;
		final String mSource = item.getSite();
		holder.mCancelBtn.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				AlertDialog.Builder builder = new AlertDialog.Builder(
						mContext);
				builder.setMessage( String.format( mContext.getResources().getString(R.string.mybinding_cancel_dialog_message), mSource ) );
				builder.setTitle( mContext.getResources().getString( R.string.mybinding_cancel_dialog_title ) );
				builder.setPositiveButton( mContext.getResources().getString( R.string.mybinding_cancel_dialog_button_yes ),
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								BindingDBEngine mEngine = new BindingDBEngine(mContext);
								mEngine.removeBindingItem(type);
								removeItem(pos);
								notifyDataSetChanged();
								dialog.dismiss();
							}
						});

				builder.setNegativeButton( mContext.getResources().getString( R.string.mybinding_cancel_dialog_button_no ),
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.dismiss();
							}
						});
				builder.create().show();
			}
		});
		return convertView;
	}
}